<?php
if(!defined('IN_ORA')) {
	exit('Access Denied');
}

class search extends mod {
	function __construct(){
		parent::__construct();
		$pid= intval($GLOBALS['_param']['p']);
		$pid= $pid>0?$pid:1;
	}
	/*** 搜索 ***/
	function v(){
		$key  = $GLOBALS['_param']['key'];
		/** 转码以防止IIS环境下可能导致的乱码问题 **/
		if(!preg_match('/.*?(Apache|kangle).*?/i',$_SERVER['SERVER_SOFTWARE'])){
			$key = iconv("gb2312",C("charset"),$key);
		}
		$key = clear_html($key);
		$key = preg_replace('/\'|\"|\=|update|select|delete|truncate|insert|drop|exec|eval|\$|\{|\}|\(|\)/i','',$key);
		if($key==""){
			redirect("index-index");
		}
		import("lib.page");
		if(!$this->isCached("search-v-key-$key-p-$pid")){
			/** 站点信息 **/
			$this->assign('sinfo',$this->db->getOne('select * from '.tn('info').' order by Id asc limit 1'));

			/** 导航信息 **/
			$this->assign('navs',$this->db->getAll('select * from '.tn('nav').' order by nav_sort desc'));


			/** 产品信息 **/
			import("lib.cat");
			$cates = cat_lib::getSubChild(0,2,' and cat_type=1');
			$this->assign('cates',$cates);
			/** 当前位置 **/
			$local = array();
			$local[] = array('url'=>get_url(true),'text'=>'产品搜索_'.$key);
			$this->assign('local',$local);
			/** 专题标签 **/
			$this->assign('tags',$this->db->getAll('select * from '.tn('tags').' where tag_type=1 group by tag_name order by Id desc'));

			/******** Search ********/
			$this->assign('keywords',$key);
			$psize = $GLOBALS['_param']['psize']?$GLOBALS['_param']['psize']:$GLOBALS['ora_cfg']['page_size'];
			$total = $this->db->count("select count(*) from ".tn('product')." where pro_status > 0 and ( instr(pro_name,'$key') or instr(pro_keywords,'$key'))");
			$pobj = new page('p',$pid,$total,$psize);
			$sql = "select op.*,oc.cat_name from  ".tn("product")." op inner join ( select Id from  ".tn("product");
			$sql .= " where pro_status > 0 and ( instr(pro_name,'$key') or instr(pro_keywords,'$key'))";
			$sql .= " order by pro_status desc limit ".$pobj->sqlParam.") ";
			$sql .= " b using(Id) left join ".tn("cat")." oc on op.cat_id = oc.Id";
			if($pobj->rsort){
				$sql = "select op.*,oc.cat_name from  ".tn("product")." op inner join ( select Id from  ".tn("product");
				$sql .= " where pro_status > 0 and ( instr(pro_name,'$key') or instr(pro_keywords,'$key'))";
				$sql .= " order by pro_status asc limit ".$pobj->sqlParam.") ";
				$sql .= " b using(Id) left join ".tn("cat")." oc on op.cat_id = oc.Id";
			}
			$this->assign("pobj",$pobj);
			$url = get_urlStr();
			$this->assign('url',preg_replace('/\-?p(\-|_|\=)\d+/i',"",$url));
			$this->assign('pros',$this->db->getAll($sql));
			$this->assign('title',array_reverse($local));
		}

		$this->display("search.html");
	}
}

?>